package com.szh.taobaounion.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szh.taobaounion.Presenter.impl.TicketPresenterImpl;
import com.szh.taobaounion.R;
import com.szh.taobaounion.base.BaseActivity;
import com.szh.taobaounion.model.domain.TicketResult;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.PresenterManager;
import com.szh.taobaounion.utils.ToastUtil;
import com.szh.taobaounion.utils.UrlUtils;
import com.szh.taobaounion.view.ITicketPagerCallBack;

import butterknife.BindView;

public class TicketActivity extends BaseActivity implements ITicketPagerCallBack {
    private boolean mHasTaoBao=false;

    private TicketPresenterImpl mTicketPresenter;
    @BindView(R.id.ticket_cover)
    ImageView mCover;

    @BindView(R.id.ticket_back_iv)
    ImageView backIv;

    @BindView(R.id.ticket_code)
    EditText mTicketCode;

    @BindView(R.id.ticket_copy_or_open_btn)
    TextView mTicketCopyOrOpen;

    @BindView(R.id.ticket_cover_loading)
    View  loadingView;

    @BindView(R.id.ticket_load_retry)
    TextView  loadRetry;
    @Override
    protected void initPersent() {
        mTicketPresenter = PresenterManager.getInstance().getmTicketPresenter();
        if (mTicketPresenter != null) {
            mTicketPresenter.registerViewCallBack(this);
        }
        //判断是否安装淘宝
        //包名：com.taobao.taobao
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mHasTaoBao=packageInfo!=null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            mHasTaoBao=false;
        }
        LogUtils.d(this,"mHasTaoBao---->"+mHasTaoBao);
         //根据这个值修改uI
        mTicketCopyOrOpen.setText(mHasTaoBao?"打开淘宝领券":"复制淘口令");

    }

    @Override
    protected void relaese() {
        if (mTicketPresenter != null) {
            mTicketPresenter.unregisterViewCallBack(this);
        }
    }

    @Override
    protected void initEvent() {
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTicketCopyOrOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //复制口令
                //拿到数据
                // act=android.intent.action.MAIN
                // cat=[android.intent.category.LAUNCHER]
                // flg=0x10200000
                // cmp=com.taobao.taobao/com.taobao.tao.welcome.Welcome
                //com.taobao.taobao/com.taobao.tao.TBMainActivity}
                // bnds=[198,780][366,948] (has extras)}
                String ticketCode = mTicketCode.getText().toString().trim();
                ClipboardManager cm= (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //复制到粘贴板
                ClipData clipdata = ClipData.newPlainText("szh_taobao_ticket_code", ticketCode);
                cm.setPrimaryClip(clipdata);
                //判断有没有淘宝 有：打开 没有：提示复制成功
                if (mHasTaoBao) {
                    Intent taobaoIntent = new Intent();
//                    taobaoIntent.setAction("android.intent.action.MAIN");
//                    taobaoIntent.addCategory("android.intent.category.LAUNCHER");
                    ComponentName componentName=new ComponentName("com.taobao.taobao","com.taobao.tao.TBMainActivity");
                    taobaoIntent.setComponent(componentName);
                    startActivity(taobaoIntent);

                }else {
                    ToastUtil.ShowToast("已经复制，粘贴分享，或打开淘宝");
                }
            }
        });
    }

    @Override
    protected void initView() {

    }


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ticket;
    }

    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        if (mCover != null&& !TextUtils.isEmpty(cover) ) {
            String ticketUrl = UrlUtils.getTicketUrl(cover);
            Glide.with(this).load(ticketUrl).into(mCover);
        }
        if (result != null&&result.getData().getTbk_tpwd_create_response()!=null) {
            String model = result.getData().getTbk_tpwd_create_response().getData().getModel();
            LogUtils.d(this,"model---->"+model);
            mTicketCode.setText(model);
        }
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }

    }

    @Override
    public void onError() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
        if (loadRetry != null) {
            loadRetry.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoading() {
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }
        if (loadRetry != null) {
            loadRetry.setVisibility(View.GONE);
        }
    }

    @Override
    public void onEmpty() {

    }

}
